package com.fmi.timedealer.achievement;

import com.fmi.timedealer.R;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ManageLevelActivity extends Activity {
	
	private AchievementLevel currentLevel = null;
	private int achievementId;
	private IDataManager dbManager = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.manage_level);
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		currentLevel = (AchievementLevel)extras.getSerializable(AchievementsActivity.LEVEL_BUNDLE_KEY);
		achievementId = extras.getInt(AchievementsActivity.ACHIEVEMENT_ID_BUNDLE_KEY);
		fillViewsData(currentLevel);
		
		Button btnSave = (Button)findViewById(R.id.btn_save_level);
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				updateCurrentLevelValues();
				if (currentLevel.getId() == 0) {
					dbManager.insertLevel(currentLevel, achievementId);
				}
				else {
					dbManager.updateLevel(currentLevel);
				}
				returnResult(Activity.RESULT_OK);
			}
		});
		
		Button btnCancel = (Button)findViewById(R.id.btn_cancel_level);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				returnResult(Activity.RESULT_CANCELED);
			}
		});
	}
	
	private void updateCurrentLevelValues() {
			
			// Fill Level data
			TextView tvLvl = (TextView)findViewById(R.id.level_number);
			if (tvLvl != null) {
				currentLevel.setLevel(Integer.parseInt(tvLvl.getText().toString()));
			}
			
			// Fill Name data
			TextView tvLvlName = (TextView)findViewById(R.id.level_name);
			if (tvLvlName != null) {
				currentLevel.setName(tvLvlName.getText().toString());
			}
			
			// Fill Goal Message data
			TextView tvLvlGoal = (TextView)findViewById(R.id.level_goal);
			if (tvLvlGoal != null) {
				currentLevel.setGoalMessage(tvLvlGoal.getText().toString());
			}
	}
	
	private void fillViewsData(AchievementLevel level) {
		
		// Fill Level data
		TextView tvLvl = (TextView)findViewById(R.id.level_number);
		if (tvLvl != null) {
			tvLvl.setText(Integer.toString(level.getLevel()));
		}
		
		// Fill Name data
		TextView tvLvlName = (TextView)findViewById(R.id.level_name);
		if (tvLvlName != null) {
			tvLvlName.setText(level.getName());
		}
		
		// Fill Goal Message data
		TextView tvLvlGoal = (TextView)findViewById(R.id.level_goal);
		if (tvLvlGoal != null) {
			tvLvlGoal.setText(level.getGoalMessage());
		}
	}
	
	private void returnResult(int resultCode) {

		if (resultCode == RESULT_OK) {
			getIntent().removeExtra(AchievementsActivity.LEVEL_BUNDLE_KEY);
			getIntent().putExtra(AchievementsActivity.LEVEL_BUNDLE_KEY, currentLevel);
		}
		this.setResult(resultCode, getIntent());
		this.finish();
	}

}
